package com.hzit.controller;

import com.hzit.dto.StudentDTO;
import com.hzit.entity.Classes;
import com.hzit.service.IClassesService;
import com.hzit.service.IStudentService;
import com.hzit.util.PageResult;
import com.hzit.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Controller
@RequestMapping("/student")
public class StudentController {

	@Autowired
	private IStudentService studentService;
	@Autowired
	private IClassesService classesService;

	//1. 查询所有学生列表
	@GetMapping("/list")
	public String findAll(Model model){
		//1.1 查询学生列表
		List<StudentDTO> studentDTOList = studentService.findAll();
		//1.2 将学生列表放到model中
		model.addAttribute("students",studentDTOList);
		return "index";
	}
	//2. 分页查询
	@GetMapping("/findPage/{page}/{pageSize}")
	public String findPage(Model model,
	                       @PathVariable int page,
	                       @PathVariable  int pageSize){
		PageResult<StudentDTO> pr = studentService.findPage(page,pageSize);
		List<Classes> classes = classesService.list();
		model.addAttribute("pr",pr);
		model.addAttribute("classes",classes);
		return "index";
	}
	//3. 添加学生
	@PostMapping("/add")
	public String add(StudentDTO dto){
		studentService.insert(dto);
		return "redirect:/student/findPage/1/5";
	}
	//4. 修改学生
	@PostMapping("/update")
	public String update(StudentDTO dto){
		studentService.update(dto);
		return "redirect:/student/findPage/1/5";
	}
	//5. 删除学生
	@GetMapping("/delete/{sid}")
	public String delete(@PathVariable Integer sid){
		studentService.removeById(sid);
		return "redirect:/student/findPage/1/5";
	}
}
